﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.Mvc;
using Xyz.Web.Reservation.CustomerService;
using Xyz.Web.Reservation.Models;
using Xyz.Web.Reservation.Repositories;

namespace Xyz.Web.Reservation.Controllers
{
    public class PaymentController : Controller
    {
        //
        // GET: /Payment/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult CardPayment()
        {
            SetViewData();
            return View(new CardPaymentModel());
        }

        [HttpPost]
        public ActionResult CardPayment(CardPaymentModel model)
        {
            if (ModelState.IsValid)
            {
                //var customer = Session["Customer"] == null ? null : Session["Customer"] as CustomerDTO;
                var bookingModel = ((BookingModel) Session["booking"]);

                var cardPayment = model.FromModel();
                cardPayment.BookingId = bookingModel.BookingId;
                cardPayment.PaidAmount = bookingModel.TotalTicketFare;

                try
                {
                    PaymentRepository.CreatePayment(cardPayment, bookingModel.FromModel());
                    return RedirectToAction("PaymentConfirmed", "Payment");
                }
                catch (FaultException ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            SetViewData();

            return View(model);
        }

        public ActionResult PaymentConfirmed()
        {
            return View();
        }

        [HttpGet]
        public ActionResult MemberPayment()
        {
            var bookingModel = ((BookingModel)Session["booking"]);

            //TODO: Probably need to change this, display the total points deducted after payment
            decimal paymentPoints = Math.Round(bookingModel.TotalTicketFare * 1.5m);

            ViewData["PaymentPoints"] = paymentPoints;
            return View(new MemberPaymentModel());
        }

        [Authorize(Roles = "Member")]
        [HttpPost]
        public ActionResult MemberPayment(MemberPaymentModel model)
        {
            var bookingModel = ((BookingModel) Session["booking"]);
            var customer = Session["Customer"] == null ? null : Session["Customer"] as CustomerModel;
            var memberPayment = model.FromModel();

            bookingModel.CustomerId = customer.CustomerId;
            memberPayment.MemberId = ((MemberModel) customer).MemberId;
            memberPayment.BookingId = bookingModel.BookingId;
            memberPayment.PaymentPoints = bookingModel.TotalTicketFare;
            memberPayment.PaymentDate = DateTime.Now;

            try
            {
                PaymentRepository.CreatePayment(memberPayment, bookingModel.FromModel());
                return RedirectToAction("PaymentConfirmed", "Payment");
            }
            catch (FaultException ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return View(model);
        }

        private void SetViewData()
        {
            ViewData["PaymentMethods"] = PaymentRepository.GetPaymentMethods().ToModel();
            ViewData["CardTypes"] = PaymentRepository.GetCardTypes().ToModel();
        }
    }
}
